import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';


import '../../../models/WifiInfo.dart';
import '../../../routes/app_pages.dart';
import '../../../style/app_colors.dart';
import '../controllers/wifi_setting_controller.dart';

class WifiSettingView extends GetView<WifiSettingController> {
  const WifiSettingView({super.key});
  @override
  Widget build(BuildContext context) {
    return Obx(() => Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Image.asset('assets/images/ic_back2.png',width: 48.w,height: 48.h),
          onPressed: () {
            Get.back();
          },
          tooltip: MaterialLocalizations.of(context).backButtonTooltip, // 可选：设置工具提示
        ),
      ),
      body: Container(
        width: Get.width,
        padding: EdgeInsets.only(top: 4.h,left: 20.w,right: 20.w,bottom: 20.h),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text('请选择Wi-Fi',style: TextStyle(fontSize: 48.sp,color: AppColors.Page_Title_Color),),
            Text('请选择安全的Wi-Fi进行连接',style: TextStyle(fontSize: 28.sp,color: AppColors.newEditFieldHintText_color)),
            controller.wifiInfos.value.length == 0 ? Text('Loading ... ') :
            Expanded(child: ListView.separated(
              itemBuilder: (context, index) {
                WifiInfo item = controller.wifiInfos.value[index];
                // if (capabilities.contains("WPA") || capabilities.contains("wpa") || capabilities.contains("WEP") || capabilities.contains("wep")) {
                bool isFree = false;
                if(item.c!.contains('WPA') || item.c!.contains('wpa') ||
                    item.c!.contains('WEP') || item.c!.contains('wep')) {
                  isFree = false;
                } else {
                  isFree = true;
                }

                return GestureDetector(
                  onTap: () {
                    if (isFree) {
                      controller.configFreeWiFi(item);
                    } else {
                      Get.toNamed(Routes.WIFI_PASSWORD,arguments: item);
                    }

                  },
                  child: Container(
                    color: Colors.white.withOpacity(0),
                    padding: EdgeInsets.all(10),
                    child: Row(
                      children: [
                        Image.asset('assets/images/wifi_icon.webp',width: 40.w,height: 40.h),
                        !isFree ? SizedBox(width: 20.w,): SizedBox(),
                        isFree? SizedBox(width: 56.w,): Image.asset('assets/images/wifi_need_pw.webp',width: 36.w,height: 36.h),
                        SizedBox(width: 30.w,),
                        Text(item.s!,style: TextStyle(fontSize: 32.sp)),
                      ],
                    ),
                  ),
                );

              },
              itemCount: controller.wifiInfos.value.length,
              separatorBuilder: (context, index) => Divider(color: AppColors.LIST_VIEW_SEPERATED_Color,height:2, thickness:0.5,indent: 6.0,endIndent: 6.0),
            ))
          ],
        ),
      ),
    ));
  }
}
